<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
<!--          <a-col :span="24">
            <a-form-model-item label="奖励规则父级id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
              <a-input v-model="model.parentId" placeholder="请输入奖励规则父级id"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="等级任务" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskCode">
<!--              <a-input v-model="model.taskCode" placeholder="请输入任务编号"  ></a-input>-->
              <j-dict-select-tag :disabled="model.id" @getKeyValue="selectChange" v-model="model.taskCode" placeholder="请选择等级任务" dictCode="level_tasks"/>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskName">
              <a-input v-model="model.taskName" placeholder="请输入任务名称"  ></a-input>
            </a-form-model-item>
          </a-col>-->
<!--          <a-col :span="24">
            <a-form-model-item label="任务描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskDescribe">
              <a-input v-model="model.taskDescribe" placeholder="请输入任务描述"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24" v-if="model.taskCode=='2'">
            <a-form-model-item label="累计推广奖励达到" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <a-input v-model="model.value" placeholder="请输入钻石数"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'LevelTasksForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           taskCode: [
              { required: true, message: '请选择等级任务!'},
           ],
          value: [
            { required: true, message: '请输入累计推广奖励!'},
          ],
        },
        url: {
          add: "/system/levelTasks/add",
          edit: "/system/levelTasks/edit",
          queryById: "/system/levelTasks/queryById"
        },
        dataList:[],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (record) {
        this.edit(record);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {

            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            for(let item in this.dataList){
              if(this.dataList[item].taskCode==this.model.taskCode){
                this.$message.warning('该等级任务已存在!');
                return false;
              }
            }
            that.confirmLoading = true;
            //修改任务描述
            if(this.model.taskCode=='1'){
              this.model.taskDescribe=this.model.taskName;
            }else if(this.model.taskCode=='2'){
              this.model.taskDescribe='累计推广奖励达到 '+this.model.value+' 钻石';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },

      selectChange(item){
        this.model.taskName=item.text;
      }
    }
  }
</script>